﻿using BaseUI;
using BLL;
using Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using Utility;

namespace Thksoft.Module.ProjectDetails
{
    public partial class IncomeBusiness1 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //判断是否是只查询
                if (Request.QueryString["Select"] == "sel")
                {
                    //btnSum.Style.Add("display", "none");
                    btnOK.Style.Add("display", "none");
                    //gvMonthPlan.Enabled = false;
                    mytr.Attributes.Add("style", "display:none");
                    btnDaochu.CssClass = "bt_css_s";
                }

                if (Request.QueryString["Ajax"] == "1")
                {
                    IResonResult result = null;
                    try
                    {
                        var jsonStr = "";
                        //读取JSON流
                        using (var stream = new System.IO.StreamReader(Request.InputStream))
                        {
                            jsonStr = stream.ReadToEnd();
                            if (string.IsNullOrEmpty(jsonStr))
                            {
                                return;
                            }
                        }
                        var jobj = JObject.Parse(jsonStr);
                        switch (jobj["Code"].ToObject<string>())
                        {
                            case "Update":

                                #region MyRegion

                                {
                                    var jData = jobj["Data"];
                                    var applyId = jData["ApplyId"].ToObject<int>();
                                    var proId = jData["ProId"].ToObject<int>();
                                    var applyState = jData["ApplyState"].ToObject<int>();
                                    var bll = new QS_PLAN_DETAILS_BLL();
                                    var dt = new List<QS_PLAN_DETAILS1>();
                                    var data = jData["Data"] as JArray;
                                    //组装对象
                                    foreach (var item in data)
                                    {
                                        var entity = new QS_PLAN_DETAILS1();
                                        entity.Index = item["Index"].ToObject<int>();
                                        entity.Name = item["Name"].ToObject<string>();
                                        entity.Flow = "";
                                        //if (item["ParentIndex"].ToObject<string>() == "")
                                        //{
                                        //    entity.ParentIndex = null;
                                        //}
                                        //else
                                        //{
                                        //    entity.ParentIndex = item["ParentIndex"].ToObject<int>();
                                        //}
                                        entity.Remark = item["Remark"].ToObject<string>();
                                        if (item["SaleNumber"].ToObject<string>() == "")
                                        {
                                            entity.SaleNumber = null;
                                        }
                                        else
                                        {
                                            entity.SaleNumber = item["SaleNumber"].ToObject<int>();
                                        }
                                        entity.SumPrice = item["SumPrice"].ToObject<decimal>();
                                        if (item["UnitePrice"].ToObject<string>() == "")
                                        {
                                            entity.UnitePrice = null;
                                        }
                                        else
                                        {
                                            entity.UnitePrice = item["UnitePrice"].ToObject<decimal>();
                                        }
                                        dt.Add(entity);
                                    }
                                    if (applyState == 1)
                                    {
                                        bll.UpdateOrCreateRangeYear1(applyId, proId, dt);
                                        result = new ResonResult("提交成功", ResultType.success);
                                    }
                                    if (applyState == 2)
                                    {
                                        bll.UpdateOrCreateRangeMonth1(applyId, proId, dt);
                                        result = new ResonResult("提交成功", ResultType.success);
                                    }
                                    if (applyState == 3)
                                    {
                                        bll.UpdateOrCreateRangeWeek1(applyId, proId, dt);
                                        result = new ResonResult("提交成功", ResultType.success);
                                    }
                                    break;
                                }

                            #endregion MyRegion

                            case "Get":

                                #region MyRegion

                                {
                                    var jData = jobj["Data"];
                                    var applyId = jData["ApplyId"].ToObject<int>();
                                    var applyState = jData["ApplyState"].ToObject<int>();
                                    var sql = "SELECT T1.C_VALUE2 AS C_INDEX,T1.C_VALUE3 AS C_NAME,T1.C_VALUE4 AS SaleNumber,T1.C_VALUE5 AS UnitePrice,T1.C_VALUE6 AS SumPrice,T1.C_VALUE7 AS Flow,T1.C_VALUE8 AS Remark,T1.C_VALUE9 AS ParentIndex FROM QS_PLAN_DETAILS T1 WHERE T1.N_PLAN_ID=";
                                    sql += applyId;
                                    sql += " AND T1.N_STATE=" + applyState;
                                    sql += " AND T1.C_VALUE1='T1'";
                                    DataTable dt = new BaseBLL().Query(sql);
                                    //ViewState["database"] = dt;
                                    result = new ResonResult("获取成功", ResultType.success, dt);
                                    break;
                                }

                            #endregion MyRegion

                            default:
                                break;
                        }
                        if (result == null)
                        {
                            result = new ResonResult("没有找到接口");
                        }
                    }
                    catch (Exception)
                    {
                        result = new ResonResult("接口异常");
                    }
                    Response.ContentType = "application/json";
                    Response.Write(JsonConvert.SerializeObject(result));
                    Response.Flush();
                    Response.End();
                    return;
                }
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaochu_Click(object sender, EventArgs e)
        {
            var applyId = Convert.ToInt32(Request.QueryString["ApplyId"]);
            var applyState = Request.QueryString["State"];
            var sql = "SELECT T1.C_VALUE2 AS C_INDEX,T1.C_VALUE3 AS C_NAME,T1.C_VALUE4 AS SaleNumber,T1.C_VALUE5 AS UnitePrice,T1.C_VALUE6 AS SumPrice,T1.C_VALUE7 AS Flow,T1.C_VALUE8 AS Remark,T1.C_VALUE9 AS ParentIndex FROM QS_PLAN_DETAILS T1 WHERE T1.N_PLAN_ID=";
            sql += applyId;
            sql += " AND T1.N_STATE=" + applyState;
            sql += " AND T1.C_VALUE1='T1'";
            DataTable dt = new BaseBLL().Query(sql);
            //DataTable dt = (DataTable)ViewState["database"];
            dt.Columns["C_INDEX"].ColumnName = "序号";
            dt.Columns["C_NAME"].ColumnName = "项目名称";
            dt.Columns["SaleNumber"].ColumnName = "销售量";
            dt.Columns["UnitePrice"].ColumnName = "单价";
            dt.Columns["SumPrice"].ColumnName = "金额";
            dt.Columns["Flow"].ColumnName = "银行现金流向";
            dt.Columns["Remark"].ColumnName = "备注";
            dt.Columns["ParentIndex"].ColumnName = "上一级序号";
            toExecl1(dt, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "主营业务收入明细表");
        }
    }
}